# require packages
require(raster)
require(rasterVis)
require(RColorBrewer)
require(sp)
require(rgdal)


#### avoided deforestation
# read in files
r1 = raster("D:/Research/global_refor_mac/results/shp_out/avoided_def_50dol_final_b.tif")
s1 = readOGR("D:/Research/global_refor_mac/data/continent",
             "cntry_adm0")

l1 = levelplot(r1, 
               col.regions=colorRampPalette(brewer.pal(9, 'YlOrBr')[c(1,5:9)]),
          #par.settings=BTCTheme, 
          margin = FALSE,
          labels = FALSE,
          ylab = NULL,
          xlab=NULL, 
          scales=list(x=list(draw=FALSE),
                      y=list(draw=FALSE)),
          maxpixels= 8e5) +
  layer(sp.polygons(s1, lwd = 0.5, col = 'gray'))

png(file=paste0("D:/Research/global_refor_mac/graphics/avoided_def_50dol.png",
                ".png"), 
    width=11000,height=9600,res=1850, units="px")
print(l1)
dev.off()


#### avoided deforestation
# read in files
r1 = raster("D:/Research/global_refor_mac/results/shp_out/def_bau_final_b.tif")

l1 = levelplot(r1, 
               col.regions=colorRampPalette(brewer.pal(9, 'YlOrBr')[c(1,5:9)]),
               margin = FALSE,
               labels = FALSE,
               ylab = NULL,
               xlab=NULL, 
               scales=list(x=list(draw=FALSE),
                           y=list(draw=FALSE)),
               maxpixels= 8e5)  +
  layer(sp.polygons(s1, lwd = 0.5, col = 'gray'))

png(file=paste0("D:/Research/global_refor_mac/graphics/def_bau_final.png",
                ".png"), 
    width=11000,height=9600,res=1850, units="px")
print(l1)
dev.off()


#### avoided deforestation
# read in files
r1 = raster("D:/Research/global_refor_mac/results/shp_out/ref_bau_final_b.tif")

l1 = levelplot(r1, 
               col.regions=colorRampPalette(brewer.pal(9, 'Greens')[c(1,5:9)]),
               margin = FALSE,
               labels = FALSE,
               ylab = NULL,
               xlab=NULL, 
               scales=list(x=list(draw=FALSE),
                           y=list(draw=FALSE)),
               maxpixels= 1e5)  +
  layer(sp.polygons(s1, lwd = 0.5, col = 'gray'))

png(file=paste0("D:/Research/global_refor_mac/graphics/ref_bau_final",
                ".png"), 
    width=11000,height=9600,res=1850, units="px")
print(l1)
dev.off()


#### avoided deforestation
# read in files
r1 = raster("D:/Research/global_refor_mac/results/shp_out/enhanced_rem_ref_50dol_final_b.tif")

l1 = levelplot(r1, 
               col.regions=colorRampPalette(brewer.pal(9, 'Greens')[c(1,5:9)]),
               margin = FALSE,
               labels = FALSE,
               ylab = NULL,
               xlab=NULL, 
               scales=list(x=list(draw=FALSE),
                           y=list(draw=FALSE)),
               maxpixels= 1e5) +
  layer(sp.polygons(s1, lwd = 0.5, col = 'gray'))

png(file=paste0("D:/Research/global_refor_mac/graphics/enhanced_rem_ref_50dol_final.png",
                ".png"), 
    width=11000,height=9600,res=1850, units="px")
print(l1)
dev.off()



#### avoided deforestation
# read in files
r1 = raster("D:/Research/global_refor_mac/results/shp_out/avoided_def_20dol_final_b.tif")
s1 = readOGR("D:/Research/global_refor_mac/data/continent",
             "cntry_adm0")

library(viridis)


l1 = levelplot(r1, 
               col.regions=colorRampPalette(c("white",brewer.pal(9, 'YlOrBr')[c(5:9)])),
               #par.settings=BTCTheme, 
               margin = FALSE,
               labels = FALSE,
               ylab = NULL,
               xlab=NULL, 
               scales=list(x=list(draw=FALSE),
                           y=list(draw=FALSE)),
               maxpixels= 8e5) +
  layer(sp.polygons(s1, lwd = 0.18, col = 'gray'))

png(file=paste0("D:/Research/global_refor_mac/graphics/avoided_def_20dol",
                ".png"), 
    width=11000,height=9600,res=1850, units="px")
print(l1)
dev.off()



#### avoided deforestation
# read in files
r1 = raster("D:/Research/global_refor_mac/results/shp_out/enhanced_rem_ref_20dol_final_b.tif")

l1 = levelplot(r1, 
               col.regions=colorRampPalette(c("white",brewer.pal(9, 'Greens')[c(1,5:9)])),
               margin = FALSE,
               labels = FALSE,
               ylab = NULL,
               xlab=NULL, 
               scales=list(x=list(draw=FALSE),
                           y=list(draw=FALSE)),
               maxpixels= 8e5) +
  layer(sp.polygons(s1, lwd = 0.18, col = 'gray'))

png(file=paste0("D:/Research/global_refor_mac/graphics/enhanced_rem_ref_20dol_final",
                ".png"), 
    width=11000,height=9600,res=1850, units="px")
print(l1)
dev.off()
